package com.yuexia.sell.converter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yuexia.sell.dataOjbect.OrderDetail;
import com.yuexia.sell.dto.OrderDTO;
import com.yuexia.sell.from.OrderFrom;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author itmrchen
 * @date 2018/7/13 0013 下午 1:28
 * @desc ctrate by YUE XIA
 */
@Slf4j
public class OrderFromToOrderDTOConverter {

    public static OrderDTO converter(OrderFrom orderFrom){
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setBuyerName(orderFrom.getName());
        orderDTO.setBuyerPhone(orderFrom.getPhone());
        orderDTO.setBuyerAddress(orderFrom.getAddress());
        orderDTO.setBuyerOpenid(orderFrom.getOpenid());

        Gson gson = new Gson();
        List<OrderDetail> orderDetailList = null;
        try {
            orderDetailList = gson.fromJson(orderFrom.getItems(),
                    new TypeToken<List<OrderDetail>>(){}.getType());
        }catch (Exception e){
            log.error("[类型转换异常] json转换对象错误！  转换的字符串为 {}",orderFrom.getItems());
        }
        orderDTO.setOrderDetailList(orderDetailList);

        return orderDTO;
    }
}
